package com.wuyb.util;

import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * Redis工具类
 *
 * @Author: wyb
 * @Date: 2024/01/03/15:04
 * @Description:
 */
@Component
public class RedisUtil {
    public static final String KEY_ONLINE = "keyOnline";

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    public void put(String key, String hashKey, Object value) {

        redisTemplate.opsForHash().put(key, hashKey, value);
    }

    public Object get(String key, String hashKey) {

        return redisTemplate.opsForHash().get(key, hashKey);
    }

    public void delete(String key, String hashKey) {

        redisTemplate.opsForHash().delete(key, hashKey);
    }


    /**
     * 保存Token到Redis中
     * @param userId
     * @param token
     */
    public void putUserOnline(Integer userId, String token) {

        put(KEY_ONLINE, userId.toString(), token);
    }

    /**
     * 从Redis中删除Token
     * @param userId
     */

    public void deleteUserOnline(Integer userId) {
        delete(KEY_ONLINE, String.valueOf(userId));
    }

    /**
     * 从Redis中获取Token
     * @param userId
     * @return
     */
    public String getUserOnline(Integer userId) {

        return (String) get(KEY_ONLINE, userId.toString());
    }

}
